package com.luxoft.scoremanager.server.dao.hibernateimpl;


import com.luxoft.scoremanager.server.dao.ScorePackageDao;
import com.luxoft.scoremanager.server.dao.exception.HibernateRecordException;
import com.luxoft.scoremanager.server.model.entity.ScorePackage;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class HibernateScorePackageDao implements ScorePackageDao {

    private static Logger LOG = Logger.getLogger(HibernateScorePackageDao.class);

    @Autowired
    private SessionFactory sessionFactory;

    public HibernateScorePackageDao() {
    }

    @Override
    public void addPackage(ScorePackage scorePackage) {
        Session session = null;
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.saveOrUpdate(scorePackage);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOG.error("Error score package save");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void updatePackage(ScorePackage scorePackage) {
        Session session = null;
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.update(scorePackage);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOG.error("Error score package update");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void deletePackage(ScorePackage scorePackage) {
        Session session = null;
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            session.beginTransaction();
            session.delete(scorePackage);
            session.getTransaction().commit();
        } catch (Exception e) {
            LOG.error("Error component delete");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
    }

    @Override
    public List<ScorePackage> getAll() {
        Session session = null;
        List scorePackageDaoModelList = new ArrayList<ScorePackage>();
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            scorePackageDaoModelList = session.createCriteria(ScorePackage.class).list();
        } catch (Exception e) {
            LOG.error("Error score package get all");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return scorePackageDaoModelList;
    }

    @Override
    public ScorePackage getById(int id) {
        Session session = null;
        ScorePackage scorePackage = new ScorePackage();
        try {
            //session = HibernateUtil.getSessionFactory().openSession();
            session = sessionFactory.openSession();
            scorePackage = (ScorePackage) session.get(ScorePackage.class, id);

        } catch (Exception e) {
            LOG.error("Error component get by id");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return scorePackage;

    }
}
